package net.esj.basic.core.itemtemplate.service.factory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import net.esj.basic.core.engine.factory.bean.BeanFactory;
import net.esj.basic.core.view.tools.searcher.Searcher;
import net.esj.basic.utils.Pagination;
import net.esj.basic.core.business.po.item.ItemDepender;
import net.esj.basic.core.business.po.item.ItemTemplate;
import net.esj.basic.core.business.po.item.ItemTemplateVo;
import net.esj.basic.core.itemtemplate.ItemTemplateConstants;
import net.esj.basic.core.itemtemplate.service.ItemTemplateService;

import org.apache.commons.lang.StringUtils;

@Singleton
@Named(ItemTemplateConstants.ItemTemplateServiceFactory_SINGLETON_NAME)
public class ItemTemplateServiceFactory {

	private static BeanFactory beanFactory;

	private static List<ItemTemplateService> services = new ArrayList<ItemTemplateService>();
	
	@Inject
	public void setBeanFactory(BeanFactory beanFactory) {
		this.beanFactory = beanFactory;
	}
	
	public static Pagination<ItemDepender> pageItemDepender(Pagination pagination,String mtype){
		return getService(mtype).pageItemDepender(pagination);
	}
	
	public static List<ItemDepender> listItemDependers(Searcher searcher,String mtype){
		return getService(mtype).listItemDependers(searcher);
	}
	
	public static ItemDepender getItemDepender(Serializable id,String mtype){
		return getService(mtype).getItemDepender(id);
	}
	
	public static Pagination<ItemTemplate> pageItemTemplate(Pagination pagination,String mtype){
		return getService(mtype).pageItemTemplate(pagination);
	}
	
	public static List<ItemTemplate> listItemTemplates(Searcher searcher,String mtype){
		return getService(mtype).listItemTemplates(searcher);
	}
	
	public static ItemTemplate getItemTemplate(Serializable id,String mtype){
		return getService(mtype).getItemTemplate(id);
	}
	
	private static ItemTemplateService getService(String mtype){
		for(ItemTemplateService service:services){
			if(StringUtils.equals(service.getMType(), mtype)){
				return service;
			}
		}
		return EMPTY_SERVICE;
	}
	
	@PostConstruct
	private void PostConstruct(){
		Map<String,ItemTemplateService> map = beanFactory.getBeansByType(ItemTemplateService.class);
		for(ItemTemplateService service : map.values()){
			if(service!=null){
				services.add(service);
			}
		}
	}
	
	private static ItemTemplateService EMPTY_SERVICE = new ItemTemplateService() {
		
		@Override
		public String getMType() {
			return "EMPTY";
		}
		
		@Override
		public Pagination<ItemTemplate> pageItemTemplate(Pagination pagination) {
			return Pagination.EMPTY_PAGE;
		}
		
		@Override
		public Pagination<ItemDepender> pageItemDepender(Pagination pagination) {
			return Pagination.EMPTY_PAGE;
		}
		
		@Override
		public List<ItemTemplate> listItemTemplates(Searcher searcher) {
			return Collections.EMPTY_LIST;
		}
		
		@Override
		public List<ItemDepender> listItemDependers(Searcher searcher) {
			return Collections.EMPTY_LIST;
		}
		
		@Override
		public ItemTemplate getItemTemplate(Serializable id) {
			return ItemTemplateVo.EMPTY;
		}
		
		@Override
		public ItemDepender getItemDepender(Serializable id) {
			return null;
		}
	};
}
